﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotaHelp.Models;
using DotaHelp.ViewModels;
using DotaHelp.Helpers;

namespace DotaHelp.Controllers
{

    
    public class DictionaryController : Controller
    {
        DotaModel model = new DotaModel();

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            model.Dispose();
            
        }


        public ActionResult ChooseLetter()
        {
            return View(model.DictionaryWords.Where(r=>!r.IsDeleted).Select(w => w.Word.Substring(0, 1).ToUpper()).Distinct().OrderBy(l => l).ToArray());
        }

        [Sitemap]
        public ActionResult List(string group, string letter)
        {
            var vm = from w in model.DictionaryWords
                      where w.IsDeleted == false
                      group w by w.Group into g
                      orderby g.Key
                      select new DictionaryListViewModel
                      {
                          Group = g.Key,
                          Words = g,
                          Comments = model.Comments.Where(c => c.IsDeleted == false && c.Url == "/Word/" + g.Key)
                      };

            if (letter == "~")
            {
                vm = vm.Where(r => !r.Comments.Any());
            } 
            else if (!string.IsNullOrWhiteSpace(letter))
            {
                vm = vm.Where(r => r.Words.Any(w => w.Word.StartsWith(letter)));
            }
            else if (!string.IsNullOrWhiteSpace(group))
            {
                vm = vm.Where(r => r.Group == group);
            }
            else 
            {
                vm = vm.Take(5);
            }

            ViewBag.SelectedGroup = group;

            return View(vm);
        }

        [HttpPost]
        public ActionResult Add(string words, string group)
        {
            if (string.IsNullOrEmpty(words))
            {
                return RedirectToAction("Error", "Home", new { error = "Требуется указать слово" });
            }
            

            group = group ?? Guid.NewGuid().ToString("d");
            foreach (var word in words
                .Split(',',';','.')
                .Select(_=>_.Trim().Trim('.'))
                .Where(_=>!string.IsNullOrWhiteSpace(_))
             )
            {
                if (model.DictionaryWords.Any(_ => _.IsDeleted == false && _.Word == word))
                {
                    return RedirectToAction("Error", "Home", new { error = "Уже есть такое слово" });
                }
                                
                var w = new DictionaryWord() { Word = word, Group = group, IsDeleted = false };
                model.AddToDictionaryWords(w);                
            }

            model.SaveChanges();
            return RedirectToAction("List", new { group = group });
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Delete(int id)
        {
            var word = model.DictionaryWords.First(w => w.Id == id);
            word.IsDeleted = true;
            model.SaveChanges();
            return RedirectToAction("List", new { group = word.Group }); 
        }
    }
}
